<!--
 * @Descripttion: 砍价活动
 * @Author: xiao li
 * @Date: 2021-01-19 09:27:11
 * @LastEditors: xiao li
 * @LastEditTime: 2021-06-01 16:28:41
-->
<template>
    <div class="lb-goods-bargain">
        <top-nav />
        <div class="page-main">
          <lb-tips>
            <p>砍价活动应用场景：用户选择心仪商品发起砍价活动，分享给多个好友后，通过好友的助力，使商品价格不断降低，当足够人数的好友助力砍价后，砍到商品最低价格后，用户可以按照此价格下单，获得该商品。同时帮助该用户砍价的好友，看到此活动后，也可以发起砍价，邀请更多人参与到砍价活动中，实现粉丝裂变增长的目的</p>
            <p class="space-lg"></p>
            <p>注：砍价商品原价购买的时候可以支持满减送优惠券使用、分销返现；但是以砍价价格购买时，无法参与满减送、分销返现；其他营销插件暂不支持</p>
            <p>活动进行中切勿取消员工名片，否则用户砍价成功后将不能下单购买</p>
          </lb-tips>
          <lb-button type='primary' icon="el-icon-plus" @click="$router.push(`/app/bargain/edit`)">{{$t('menu.BargainAdd')}}</lb-button>
          <div class="space-lg"></div>
          <el-row class="page-search-form">
            <el-form
            :inline="true"
            :model="searchForm"
            ref="searchForm"
            >
              <el-form-item label="输入查询" prop="title">
                <el-input v-model="searchForm.title" placeholder="请输入活动名称"></el-input>
              </el-form-item>
              <el-form-item label="活动状态" prop="status">
              <el-select v-model="searchForm.status" placeholder="请选择活动状态">
                  <el-option
                    v-for="item in statusOptions"
                    :key="item.id"
                    :label="item.title"
                    :value="item.id"
                  ></el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="">
                <lb-button size='medium' type='primary' opType='add' icon="el-icon-search" style="margin-right:5px" @click="getTableDataList(1)">{{$t('action.search')}}</lb-button>
                <lb-button size='medium' icon="el-icon-refresh-left" @click="resetForm">{{$t('action.reset')}}</lb-button>
              </el-form-item>
            </el-form>
          </el-row>
          <lb-tool-tips mode="text">
            <div class="c-warning mb-lg">成交订单数：活动页面砍价成交订单数<br>
          剩余库存：剩余库存=商品库存-成单数。当活动库存-成单数大于商品库存时，剩余库存=商品库存</div>
          </lb-tool-tips>
          <el-table
            v-loading='loading'
            :data="tableData"
          :header-cell-style="{background:'#f5f7fa',color:'#606266'}"
            tooltip-effect="dark"
            style="width: 100%"
           >
            <el-table-column
              prop="id"
              label="ID">
            </el-table-column>
            <el-table-column
              prop="title"
              label="活动名称">
            </el-table-column>
            <el-table-column
              prop="pv"
              label="浏览量">
            </el-table-column>
            <el-table-column
              prop="user_count"
              label="访客数">
            </el-table-column>
            <el-table-column
              prop="record_count"
              label="发起人数">
            </el-table-column>
            <el-table-column
              prop="record_user_count"
              label="帮砍人数">
            </el-table-column>
            <el-table-column
              prop="record_success_count"
              label="砍价成功数">
            </el-table-column>
            <el-table-column
              prop="order_count"
              label="成交订单数">
            </el-table-column>
            <el-table-column
              prop="y_stock"
              label="剩余库存">
            </el-table-column>
            <el-table-column
              prop="num"
              label="活动状态">
                <template slot-scope="scope">
                  {{statusType[scope.row.atv_status]}}
                </template>
            </el-table-column>
            <el-table-column
              prop="status"
              label="是否上架">
                <template slot-scope="scope">
                  <el-switch
                    v-model="scope.row.status"
                    :active-value="1"
                    :inactive-value="0"
                    @change="updateItem(scope.row.id,scope.row.status)"
                    >
                  </el-switch>
                </template>
            </el-table-column>
            <el-table-column
              prop=""
              label="操作">
                <template slot-scope="scope">
                  <div class="table-operate">
                    <lb-button size='mini' plain type='primary' @click="$router.push(`/app/bargain/edit?id=${scope.row.id}`)">{{$t('action.edit')}}</lb-button>
                    <lb-button size='mini' plain type='danger' @click="confirmDel(scope.row.id)">{{$t('action.delete')}}</lb-button>
                    <lb-button size='mini' plain type='success' @click="$router.push(`/app/bargain/detail?id=${scope.row.id}`)">参与情况</lb-button>
                  </div>
                </template>
            </el-table-column>
          </el-table>
      <lb-page
        :batch="false"
        :page="searchForm.page"
        :pageSize="searchForm.limit"
        :total="total"
        @handleSizeChange="handleSizeChange"
        @handleCurrentChange="handleCurrentChange"
      >
      </lb-page>
        </div>
    </div>
</template>

<script>
export default {
  data () {
    return {
      statusOptions: [{
        id: 0,
        title: '全部'
      }, {
        id: 1,
        title: '未开始'
      }, {
        id: 2,
        title: '进行中'
      }, {
        id: 3,
        title: '已结束'
      }],
      statusType: {
        1: '未开始',
        2: '进行中',
        3: '已结束'
      },
      loading: false,
      total: 0,
      searchForm: {
        page: 1,
        limit: 10,
        title: '',
        status: 0
      },
      tableData: [],
      multipleSelection: [],
      showTable: true
    }
  },
  activated () {
    this.getTableDataList()
  },
  methods: {
    /**
     * @method: 重置表单
     */
    resetForm () {
      this.$refs['searchForm'].resetFields()
      this.getTableDataList(1)
    },
    handleSizeChange (val) {
      this.searchForm.limit = val
      this.handleCurrentChange(1)
    },
    handleCurrentChange (val) {
      this.searchForm.page = val
      this.getTableDataList()
    },
    /**
     * @method 获取列表
     */
    getTableDataList (flag) {
      if (flag) this.searchForm.page = 1
      this.loading = true
      this.$api.bargainList(this.searchForm).then(res => {
        this.loading = false
        if (res.code === 200) {
          this.tableData = res.data.data
          this.total = res.data.total
        }
      })
    },
    /**
     * @method 删除
     */
    confirmDel (id) {
      this.$confirm(this.$t('tips.confirmDelete'), this.$t('tips.reminder'), {
        confirmButtonText: this.$t('action.comfirm'),
        cancelButtonText: this.$t('action.cancel'),
        type: 'warning'
      }).then(() => {
        this.updateItem(id, -1)
      }).catch(() => {

      })
    },
    /**
     * @method 上下架/删除
     */
    async updateItem (id, status) {
      this.$api.bargainStatusUpdate({id, status}).then(res => {
        if (res.code === 200) {
          this.$message.success(this.$t(status === -1 ? 'tips.successDel' : 'tips.successOper'))
          if (status !== -1) return
          this.searchForm.page = this.searchForm.page < Math.ceil((this.total - 1) / this.searchForm.limit) ? this.searchForm.page : Math.ceil((this.total - 1) / this.searchForm.limit)
          this.getTableDataList()
        } else {
          if (status === -1) return
          this.getTableDataList()
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
  .lb-goods-bargain{
    width: 100%;
    .page-main{
      padding: 20px;
      .el-row{
        margin-bottom: 20px;
        .el-button{
          margin-right: 24px;
          &:last-child{
            margin-right: 0;
          }
        }
      }
    }
  }
</style>
